var $myjq;
function getScript(){
    var script=document.createElement('script');
    script.src='//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';
    var head=document.getElementsByTagName('head')[0],
    done=false;
    // Attach handlers for all browsers
    script.onload=script.onreadystatechange = function(){
        if ( !done && (!this.readyState
            || this.readyState == 'loaded'
            || this.readyState == 'complete') ) {
            done=true;
            $myjq=jQuery.noConflict();
            window.setTimeout(quoraSort,50);
            script.onload = script.onreadystatechange = null;
            head.removeChild(script);
        }
    };
    head.appendChild(script);
}
getScript();
function getNumber(txt){
    var num = 0;
    try{
        num = parseInt(txt);
        if (num.toString() != txt) {
            num = parseFloat(txt.replace(/[kK]/g,''))*1000;
        num = isNaN(num) ? 0 : num;
    }
    }catch(e){console.log('Fail in getNumber '+e.message);}
    return num;
}
function scrollToBottom(){
    $myjq(document).scrollTop($myjq(document).scrollTop()+$myjq(document).height());
}
function sortFeed(){
    $myjq(document).scrollTop(0);
    newparent = $myjq('.pagedlist_item').parent();
    try {
        fd=$myjq('.pagedlist_item').detach();
        fd.sort(function(a,b){
            try{
                voteCnt = ($myjq('.numbers').length == 0) ? '.voter_count' : '.numbers';
                var aa = getNumber($myjq(a).find(voteCnt).text());
                var bb = getNumber($myjq(b).find(voteCnt).text());
                aa = (typeof aa !== 'undefined') ? aa : 0;
                bb = (typeof bb !== 'undefined') ? bb : 0;
                return bb-aa;
            }catch(e){console.log('Fail in sort '+e.message);}
        });
    }catch(e){
        console.log('Fail in sortFeed '+e.message);
    }finally{
        newparent.prepend(fd);
    }
}
    var pgdn = parseInt(window.prompt("Number of attempts to load more content",10)) || 0;
    var pgdnTimer = 0;
    var timedelay = 2000; //2 seconds
function quoraSort(){
    for(var i=0; i<pgdn; i++){
        window.setTimeout(scrollToBottom,pgdnTimer);
        pgdnTimer = (i+1)*timedelay;
    }
    window.setTimeout(sortFeed,pgdnTimer+100);
}